/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.org.jr4jsf.renderer;

import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import javax.faces.render.Renderer;
import sv.org.jr4jsf.component.ReportComponent;

/**
 *
 * @author steve
 */
@FacesRenderer(componentFamily = "Report", rendererType = "Report")
public class ReportRenderer extends Renderer {

    public ReportRenderer() {
    }

    @Override
    public void encodeBegin(FacesContext context, UIComponent component)
            throws IOException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }
        ReportComponent report = (ReportComponent) component;
        ResponseWriter writer = context.getResponseWriter();

        writer.startElement("div", report);
        if (component.getAttributes().containsKey("style")) {
            writer.writeAttribute("style", component.getAttributes().get("style"), "style");
        }
        writer.writeAttribute("name", report.getId(), "id");
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component)
            throws IOException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }
        ReportComponent report = (ReportComponent) component;
        ResponseWriter writer = context.getResponseWriter();
        writer.write("mi primer componente");
        writer.startElement("input", report);
        writer.writeAttribute("type", "hidden", null);
        writer.writeAttribute("name", getName(context, report), "clientId");
        writer.endElement("input");
        writer.endElement("div");
    }

    @Override
    public void decode(FacesContext context, UIComponent component) {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }
        ReportComponent report = (ReportComponent) component;
        String key = getName(context, report);
        String value = (String) context.getExternalContext().
                getRequestParameterMap().get(key);
        if (value != null) {
        }
    }

    private String getName(
            FacesContext context,
            UIComponent component) {
        return (component.getId() + "_current");
    }

    private String getURI(FacesContext context) {
        StringBuilder sb = new StringBuilder();
        sb.append(context.getExternalContext().getRequestContextPath());
        sb.append("/faces");
        sb.append(context.getViewRoot().getViewId());

        return (sb.toString());
    }
}
